package org.jaronsource.framework.core.service;

import java.io.Serializable;
import java.util.List;

import org.jaronsource.framework.core.dao.support.Criteria;
import org.jaronsource.framework.core.dao.support.IEntity;
import org.jaronsource.framework.core.dao.support.Page;
import org.jaronsource.framework.core.dao.support.PageRequest;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Qualifier ( "searchFormSupportCommonService")
@Transactional ( readOnly = true)
public class SearchFormSupportCommonService extends CommonService implements ISearchFormSupportCommonService {

	@Override
	public <T extends IEntity<I>, I extends Serializable> List<T> find( Class<T> clazz,
																		SearchForm searchForm ) {
		Criteria criteria = parseForm( searchForm );
		return commonDao.find( clazz, criteria );
	}

	@Override
	public <T extends IEntity<I>, I extends Serializable> Page<T> findPage( Class<T> clazz,
																			SearchForm searchForm ) {

		PageRequest pageRequest = searchForm.getPageRequest();

		Criteria criteria = parseForm( searchForm );

		return commonDao.findPage( clazz, pageRequest, criteria );
	}

	protected Criteria parseForm( SearchForm searchForm ) {
		return SearchFormUtils.parseForm( searchForm );
	}

}
